package com.yh.csx.bsf.flow.dao.dal;

import com.yh.csx.bsf.core.base.Ref;
import com.yh.csx.bsf.core.util.StringUtils;
import com.yh.csx.bsf.flow.core.db.ConvertUtil;
import com.yh.csx.bsf.flow.core.db.DbConn;
import com.yh.csx.bsf.flow.dao.dal.auto.t_model_base_dal;
import com.yh.csx.bsf.flow.dao.model.auto.t_model_model;
import lombok.val;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/** 代码自动生成工具自动生成,不要在这里写自己的代码，否则会被自动覆盖哦 - 车毅*/
public class t_model_dal extends t_model_base_dal
{
    public List<t_model_model> getPage(DbConn db,String name, String title, String userby,Integer modelType, Integer pageIndex, Integer pageSize, Ref<Integer> totalSize){
        val par = new ArrayList<>();
        val rs = new ArrayList<t_model_model>();

        StringBuilder sb = new StringBuilder(" from t_model where is_use=1 ");
        if(!StringUtils.isEmpty(name)){
            par.add(name);
            sb.append(" and name like concat('%', ?, '%')");
        }
        if(!StringUtils.isEmpty(title)){
            par.add(title);
            sb.append(" and title like concat('%', ?, '%')");
        }
        if(!StringUtils.isEmpty(userby)){
            par.add(userby);
            sb.append(" and update_by like concat('%', ?, '%')");
        }
        if(modelType!=null && modelType > 0) {
        	 par.add(modelType);
             sb.append(" and model_type = ?");
        }
        String sql = "select * "+sb.toString() +" order by id desc "+String.format(" limit %s,%s",(pageIndex-1)*pageSize,pageSize);
        String countSql = "select count(0) "+sb.toString();
        val ds = db.executeList(sql, par.toArray());
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(createModel(dr));
            }
        }
        totalSize.setData(ConvertUtil.objToInt(db.executeScalar(countSql,par.toArray())));
        return rs;
    }

    public List<t_model_model> getHistroy(DbConn db, String name, Integer pageIndex, Integer pageSize, Ref<Integer> totalSize){
        val par = new ArrayList<>();
        val rs = new ArrayList<t_model_model>();

        StringBuilder sb = new StringBuilder(" from t_model where name=? ");
        par.add(name);

        String sql = "select * "+sb.toString()+ " order by id desc " + String.format(" limit %s,%s",pageIndex,pageSize) ;
        String countSql = "select count(0) "+sb.toString();
        val ds = db.executeList(sql, par.toArray());
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(createModel(dr));
            }
        }
        totalSize.setData(ConvertUtil.objToInt(db.executeScalar(countSql,par.toArray())));
        return rs;
    }

    public void setUse(DbConn conn,Integer id,Boolean isuse){
        val par = new ArrayList<>();
        par.add(isuse);
        par.add(id);
        StringBuilder sb = new StringBuilder("update t_model set is_use=? where id=? ");
        conn.executeSql(sb.toString(),par.toArray());
    }


    public t_model_model get(DbConn conn, String name)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(name);
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select s.* from t_model s where s.name=?");
        List<Map<String,Object>> ds = conn.executeList(stringSql.toString(), Par.toArray());
        if (ds != null && ds.size() > 0)
        {
            return createModel(ds.get(0));
        }
        return null;
    }
    
    public Integer getModelType(DbConn conn, Integer id)
    {
        ArrayList<Object> par = new ArrayList<Object>();
        par.add(id);
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select s.model_type from t_model s where s.id=?");  
        return ConvertUtil.objToInt(conn.executeScalar(stringSql.toString(), par.toArray()));
    }
    
    
    public int getMaxVersion(DbConn conn, String name)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(name);
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select max(version) as version from t_model s where s.name=?");
        val data = conn.executeScalar(stringSql.toString(), Par.toArray());
        return ConvertUtil.objToInt(data);
    }


    public t_model_model getUse(DbConn conn, String name)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(name);
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select s.* from t_model s where s.name=? and is_use=1");
        List<Map<String,Object>> ds = conn.executeList(stringSql.toString(), Par.toArray());
        if (ds != null && ds.size() > 0)
        {
            return createModel(ds.get(0));
        }
        return null;
    }

    public boolean delete(DbConn conn, String name)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(name);

        String Sql = "delete from t_model where name=?";
        int rev = conn.executeSql(Sql, Par.toArray());
        if (rev == 1)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
}
